1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3.builder;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.fail;
21  
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  
25  import org.apache.commons.lang3.SystemUtils;
26  import org.apache.commons.lang3.builder.ToStringStyleTest.Person;
27  import org.junit.After;
28  import org.junit.Before;
29  import org.junit.Test;
30  
31  /**
32   * Unit tests {@link org.apache.commons.lang3.builder.JsonToStringStyleTest}.
33   *
34   * @version $Id$
35   */
36  public class JsonToStringStyleTest {
37  
38      private final Integer base = Integer.valueOf(5);
39  
40      @Before
41      public void setUp() throws Exception {
42          ToStringBuilder.setDefaultStyle(ToStringStyle.JSON_STYLE);
43      }
44  
45      @After
46      public void tearDown() throws Exception {
47          ToStringBuilder.setDefaultStyle(ToStringStyle.DEFAULT_STYLE);
48      }
49  
50      // ----------------------------------------------------------------
51  
52      @Test
53      public void testNull() {
54          assertEquals("null", new ToStringBuilder(null).toString());
55      }
56  
57      @Test
58      public void testBlank() {
59          assertEquals("{}", new ToStringBuilder(base).toString());
60      }
61  
62      @Test
63      public void testAppendSuper() {
64          assertEquals(
65                  "{}",
66                  new ToStringBuilder(base).appendSuper(
67                          "Integer@8888[" + SystemUtils.LINE_SEPARATOR + "]")
68                          .toString());
69          assertEquals(
70                  "{}",
71                  new ToStringBuilder(base).appendSuper(
72                          "Integer@8888[" + SystemUtils.LINE_SEPARATOR + "  null"
73                                  + SystemUtils.LINE_SEPARATOR + "]").toString());
74          assertEquals(
75                  "{\"a\":\"hello\"}",
76                  new ToStringBuilder(base)
77                          .appendSuper(
78                                  "Integer@8888[" + SystemUtils.LINE_SEPARATOR
79                                          + "]").append("a", "hello").toString());
80          assertEquals(
81                  "{\"a\":\"hello\"}",
82                  new ToStringBuilder(base)
83                          .appendSuper(
84                                  "Integer@8888[" + SystemUtils.LINE_SEPARATOR
85                                          + "  null" + SystemUtils.LINE_SEPARATOR
86                                          + "]").append("a", "hello").toString());
87          assertEquals("{\"a\":\"hello\"}", new ToStringBuilder(base)
88                  .appendSuper(null).append("a", "hello").toString());
89  
90          assertEquals("{\"a\":\"hello\",\"b\":\"world\"}", new ToStringBuilder(base)
91                  .appendSuper("{\"a\":\"hello\"}").append("b", "world").toString());
92      }
93  
94      @Test
95      public void testObject() {
96  
97          final Integer i3 = Integer.valueOf(3);
98          final Integer i4 = Integer.valueOf(4);
99  
100         try {
101             new ToStringBuilder(base).append((Object) null).toString();
102             fail("Should have generated UnsupportedOperationException");
103         } catch (UnsupportedOperationException e) {
104         }
105 
106         try {
107             new ToStringBuilder(base).append(i3).toString();
108             fail("Should have generated UnsupportedOperationException");
109         } catch (UnsupportedOperationException e) {
110         }
111 
112         assertEquals("{\"a\":null}",
113                 new ToStringBuilder(base).append("a", (Object) null).toString());
114         assertEquals("{\"a\":3}", new ToStringBuilder(base).append("a", i3)
115                 .toString());
116         assertEquals("{\"a\":3,\"b\":4}",
117                 new ToStringBuilder(base).append("a", i3).append("b", i4)
118                         .toString());
119 
120         try {
121             new ToStringBuilder(base).append("a", i3, false).toString();
122             fail("Should have generated UnsupportedOperationException");
123         } catch (UnsupportedOperationException e) {
124         }
125 
126         try {
127             new ToStringBuilder(base).append("a", new ArrayList<Object>(), false).toString();
128             fail("Should have generated UnsupportedOperationException");
129         } catch (UnsupportedOperationException e) {
130         }
131 
132         assertEquals(
133                 "{\"a\":[]}",
134                 new ToStringBuilder(base).append("a", new ArrayList<Object>(),
135                         true).toString());
136 
137         try {
138             new ToStringBuilder(base).append("a", new HashMap<Object, Object>(), false).toString();
139             fail("Should have generated UnsupportedOperationException");
140         } catch (UnsupportedOperationException e) {
141         }
142 
143         assertEquals(
144                 "{\"a\":{}}",
145                 new ToStringBuilder(base).append("a",
146                         new HashMap<Object, Object>(), true).toString());
147 
148         try {
149             new ToStringBuilder(base).append("a", (Object) new String[0], false).toString();
150             fail("Should have generated UnsupportedOperationException");
151         } catch (UnsupportedOperationException e) {
152         }
153 
154         assertEquals(
155                 "{\"a\":[]}",
156                 new ToStringBuilder(base).append("a", (Object) new String[0],
157                         true).toString());
158 
159         try {
160             new ToStringBuilder(base).append("a", (Object) new int[]{1, 2, 3}, false).toString();
161             fail("Should have generated UnsupportedOperationException");
162         } catch (UnsupportedOperationException e) {
163         }
164 
165 
166         assertEquals(
167                 "{\"a\":[1,2,3]}",
168                 new ToStringBuilder(base).append("a",
169                         (Object) new int[]{1, 2, 3}, true).toString());
170 
171         try {
172             new ToStringBuilder(base).append("a", (Object) new String[]{"v", "x", "y", "z"}, false).toString();
173             fail("Should have generated UnsupportedOperationException");
174         } catch (UnsupportedOperationException e) {
175         }
176 
177 
178         assertEquals(
179                 "{\"a\":[\"v\",\"x\",\"y\",\"z\"]}",
180                 new ToStringBuilder(base).append("a",
181                         (Object) new String[]{"v", "x", "y", "z"}, true)
182                         .toString());
183     }
184 
185     @Test
186     public void testPerson() {
187         final Person p = new Person();
188         p.name = "Jane Doe";
189         p.age = 25;
190         p.smoker = true;
191 
192         assertEquals(
193                 "{\"name\":\"Jane Doe\",\"age\":25,\"smoker\":true}",
194                 new ToStringBuilder(p).append("name", p.name)
195                         .append("age", p.age).append("smoker", p.smoker)
196                         .toString());
197     }
198 
199     @Test
200     public void testLong() {
201 
202         try {
203             new ToStringBuilder(base).append(3L).toString();
204             fail("Should have generated UnsupportedOperationException");
205         } catch (UnsupportedOperationException e) {
206         }
207 
208         assertEquals("{\"a\":3}", new ToStringBuilder(base).append("a", 3L)
209                 .toString());
210         assertEquals("{\"a\":3,\"b\":4}",
211                 new ToStringBuilder(base).append("a", 3L).append("b", 4L)
212                         .toString());
213     }
214 
215     @Test
216     public void testObjectArray() {
217         Object[] array = new Object[]{null, base, new int[]{3, 6}};
218 
219         try {
220             new ToStringBuilder(base).append(array).toString();
221             fail("Should have generated UnsupportedOperationException");
222         } catch (UnsupportedOperationException e) {
223         }
224 
225         try {
226             new ToStringBuilder(base).append((Object) array).toString();
227             fail("Should have generated UnsupportedOperationException");
228         } catch (UnsupportedOperationException e) {
229         }
230 
231         array = null;
232         try {
233             new ToStringBuilder(base).append(array).toString();
234             fail("Should have generated UnsupportedOperationException");
235         } catch (UnsupportedOperationException e) {
236         }
237 
238         try {
239             new ToStringBuilder(base).append((Object) array).toString();
240             fail("Should have generated UnsupportedOperationException");
241         } catch (UnsupportedOperationException e) {
242         }
243     }
244 
245     @Test
246     public void testLongArray() {
247         long[] array = new long[]{1, 2, -3, 4};
248 
249         try {
250             new ToStringBuilder(base).append(array).toString();
251             fail("Should have generated UnsupportedOperationException");
252         } catch (UnsupportedOperationException e) {
253         }
254 
255         try {
256             new ToStringBuilder(base).append((Object) array).toString();
257             fail("Should have generated UnsupportedOperationException");
258         } catch (UnsupportedOperationException e) {
259         }
260 
261         array = null;
262 
263         try {
264             new ToStringBuilder(base).append(array).toString();
265             fail("Should have generated UnsupportedOperationException");
266         } catch (UnsupportedOperationException e) {
267         }
268 
269         try {
270             new ToStringBuilder(base).append((Object) array).toString();
271             fail("Should have generated UnsupportedOperationException");
272         } catch (UnsupportedOperationException e) {
273         }
274     }
275 
276     @Test
277     public void testLongArrayArray() {
278         long[][] array = new long[][]{{1, 2}, null, {5}};
279 
280         try {
281             new ToStringBuilder(base).append(array).toString();
282             fail("Should have generated UnsupportedOperationException");
283         } catch (UnsupportedOperationException e) {
284         }
285 
286         try {
287             new ToStringBuilder(base).append((Object) array).toString();
288             fail("Should have generated UnsupportedOperationException");
289         } catch (UnsupportedOperationException e) {
290         }
291 
292         array = null;
293 
294         try {
295             new ToStringBuilder(base).append(array).toString();
296             fail("Should have generated UnsupportedOperationException");
297         } catch (UnsupportedOperationException e) {
298         }
299 
300         try {
301             new ToStringBuilder(base).append((Object) array).toString();
302             fail("Should have generated UnsupportedOperationException");
303         } catch (UnsupportedOperationException e) {
304         }
305     }
306 }